package com.easylinkin.linkappapi.grid.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.grid.entity.GridFile;
import com.easylinkin.linkappapi.grid.models.dto.GridFileSaveDTO;
import com.easylinkin.linkappapi.grid.models.query.GridFileQuery;
import com.easylinkin.linkappapi.grid.models.vo.GridFileInfoVO;
import com.easylinkin.linkappapi.grid.service.IGridFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


/**
 * 网格文件
 *
 * @author kuyujie
 * @since 2024-05-23
 */
@Api(tags = "网格文件")
@Setter(onMethod_ = @Resource)
@RestController
@RequestMapping("/grid/gridFile")
public class GridFileController {

    private IGridFileService gridFileService;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<GridFile> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        if (CollUtil.isNotEmpty(requestModel.getSorts())) {
            requestModel.getSorts().forEach(s -> {
                requestModel.getPage().getOrders().add(new OrderItem().setColumn(StrUtil.toUnderlineCase(s.getField())).setAsc("ASC".equalsIgnoreCase(s.getSortRule())));
            });
        } else {
            requestModel.getPage().getOrders().add(new OrderItem().setColumn("id").setAsc(false));
        }
        IPage<GridFile> pageList = gridFileService.page(requestModel.getPage(), Wrappers.lambdaQuery(requestModel.getCustomQueryParams()));
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 列表查询
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "列表查询", notes = "列表查询")
    @PostMapping(value = "/list")
    public RestMessage list(@RequestBody GridFileQuery query) {
        List<GridFileInfoVO> data = gridFileService.listInfoByQuery(query);
        return RestBuilders.successBuilder().data(data).build();
    }

    /**
     * 查询文件数量
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "查询文件数量", notes = "查询文件数量")
    @PostMapping(value = "/count")
    public RestMessage count(@RequestBody GridFileQuery query) {
        long data = gridFileService.countByQuery(query);
        return RestBuilders.successBuilder().data(data).build();
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody GridFileSaveDTO dto) {
        gridFileService.saveDate(dto);
        return RestBuilders.successBuilder().message("保存成功").build();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        gridFileService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public RestMessage deleteBatch(@RequestParam("ids") String ids) {
        this.gridFileService.removeByIds(Arrays.asList(ids.split(",")));
        return RestBuilders.successBuilder().message("批量删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Integer id) {
        GridFile gridFile = gridFileService.getById(id);
        if (gridFile == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        return RestBuilders.successBuilder().data(gridFile).build();
    }
}
